package com.cristina.attention.pending;

import com.cristina.attention.config.ThreadPoolConfig;
import com.cristina.attention.utils.GroupIdMappingUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * @author by superman
 */
@Component
public class Holder {

    /**
     * 线程池的参数
     * coreSize 核心线程数
     * maxSize 最大线程数
     * queueSize 队列大小
     */
    private Integer coreSize = 3;
    private Integer maxSize = 3;
    private Integer queueSize = 100;

    private Map<String, ExecutorService> taskPendingHolder = new HashMap<>(32);

    /**
     * 拿到所有的 groupId
     */
    private static List<String> groupIds = GroupIdMappingUtils.getAllGroupIds();

    /**
     * 给每个渠道, 每种消息类型初始化一个线程池
     * TODO 实际应该根据业务场景根据不同的 groupId 分配不同的线程数和队列大小
     */
    @PostConstruct
    public void init() {
        for (String groupId : groupIds) {
            taskPendingHolder.put(groupId, ThreadPoolConfig.getThreadPool(coreSize, maxSize, queueSize));
        }
    }

    public Map<String, ExecutorService> getTaskPendingHolder() {
        return taskPendingHolder;
    }
}
